package com.burgeon.backend.Rest.Template.Reviews;
/**
 * 按日期获取零售单
 */
import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.RetailData;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

public class FindRetailData extends RESTBase {


    private String ip;
    private String account;
    private String password;

    public FindRetailData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public ArrayList<RetailData> getData(String day) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(day), url);
        System.err.println(str);

      /*  String count = new JSONArray(str).getJSONObject(0).getString("count");*/
        ArrayList<RetailData> data = new ArrayList<RetailData>();

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        for (int i =0;i<jsonArray.length();i++){

            RetailData retailData = new RetailData();
            retailData.setId(jsonArray.getJSONArray(i).getString(0));
            retailData.setDate(jsonArray.getJSONArray(i).getString(1));
            retailData.setCode(jsonArray.getJSONArray(i).getString(2));
            retailData.setOpenid(jsonArray.getJSONArray(i).getString(3));
            retailData.setPhone(jsonArray.getJSONArray(i).getString(4));
            retailData.setStore(jsonArray.getJSONArray(i).getString(5));
            retailData.setActual(jsonArray.getJSONArray(i).getString(6));
            retailData.setAmount(jsonArray.getJSONArray(i).getString(7));
            retailData.setIntegral(jsonArray.getJSONArray(i).getString(8));

            data.add(retailData);
        }

        return data;
    }

    private MultiValueMap Data(String day) {

        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("BILLDATE");//单据日期1
        columns.add("DOCNO");//单据编号2
        columns.add("'C_VIP_ID;OPENID'");
        columns.add("'C_VIP_ID;MOBIL'");
        columns.add("'C_STORE_ID;NAME'");//消费店仓5
        columns.add("TOT_AMT_ACTUAL");//消费金额6
        columns.add("'C_VIP_ID;FA_VIPACC_ID;AMOUNT'");//余额
        columns.add("DOCNO_INTEGRAL");//获得积分

        params.put("columns", columns);
        params.put("table", "M_RETAIL");//表名

        params.put("count", "true");
        HashMap orderby = new HashMap();

        orderby.put("column", "BILLDATE");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -Integer.parseInt(day));

        Date date = c.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String sTime = sdf.format(date);
        HashMap expr1v1 = new HashMap();
        HashMap expr1v2 = new HashMap();

        expr1.put("combine", "and");
        expr1v1.put("column", "BILLDATE");
        expr1v1.put("condition", "'="+sTime +"'");
        expr1.put("expr1", expr1v1);
        expr1v2.put("column", "'C_VIP_ID;OPENID'");
        expr1v2.put("condition", "is not null");
        expr1.put("expr2", expr1v2);

        expr2.put("column", "STATUS");
        expr2.put("condition", "2");

        paramss.put("combine", "and");//关系

        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);

        params.put("params", paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindRetailData g = new FindRetailData("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("0"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
